//结构体初始化与属性继承测试
package extendtest

import (
	"fmt"
)

//	人类
type Person struct {
	name string //名称
	age  int    //年龄
}

//老师
type Teacher struct {
	Person        //匿名字段的属性会被所在的Teacher继承,但是不能覆盖已有的同名属性
	object string //科目
}

//学生
type Student struct {
	Person        //匿名字段的属性会被所在的Student继承
	name   string //名称 覆盖匿名字段的属性
}

//结构体初始化1
func test1() {
	var person Person
	person = Person{"tony", 22}            //按顺序初始化
	person = Person{age: 22, name: "tony"} //按字段名称初始化

	fmt.Println(person)
}

//结构体初始化2
func test2() {
	var person Person
	person = Person{"tony", 22}
	person = Person{age: 22, name: "tony"}

	var teacher Teacher
	teacher = Teacher{Person{"tony", 22}, "math"}
	teacher = Teacher{Person{name: "tony", age: 22}, "math"}
	teacher = Teacher{person, "math"} //拷贝person属性进行初始化

	fmt.Println(person)
	fmt.Println(teacher)
}

//继承字段测试
func test3() {
	var person Person
	person = Person{age: 22, name: "tony"} //按字段名称初始化

	var teacher Teacher
	teacher = Teacher{person, "math"} //拷贝person属性进行初始化

	person.name = "tom"
	person.age = 25

	fmt.Println(teacher.name) //tony
	fmt.Println(teacher.age)  //22

	teacher.name = "tony2"

	fmt.Println(teacher.name) //tony2
	fmt.Println(person.name)  //tom
}

//覆盖继承字段测试
func test4() {
	var person Person
	person = Person{age: 22, name: "tony"} //按字段名称初始化

	var student Student
	student = Student{person, "小明"} //拷贝person属性进行初始化

	person.name = "aaa"
	student.Person.name = "bbb" //此时匿名字段的属性不被继承
	fmt.Println(student.name)   //小明

	student.name = "ccc"
	fmt.Println(student.name) //cc
}

func TestField() {
	test4()
}
